﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_geProcMed : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Exploracao ex3;
        public Medicamento md3;

        public string data;
        
        public frm_geProcMed(Exploracao ex2, string data)
        {
            this.data = data;
            this.ex3 = ex2;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void pesquisar_medicamento()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_medicamento, nome, dosagem, tipo FROM Medicamentos WHERE nome LIKE '%" + txtgc_nome.Text + "%'";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_medicamento"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["dosagem"].ToString();
                this.dataGridView1[3, i].Value = readatrib["tipo"].ToString();
            }
        }

        private void cmdia1_pesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                pesquisar_medicamento();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdia1_continuar_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                md3 = new Medicamento(Convert.ToInt32(dataGridView1["id_medicamento", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["dosagem", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["tipo", dataGridView1.CurrentRow.Index].Value.ToString());

                frm_explvaci frm1 = new frm_explvaci(ex3, md3, "med", data);
                this.Hide();
                frm1.ShowDialog();
            }
            else
                MessageBox.Show("Tem de selecionar um medicamento!", "Seleção do Medicamento", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
